package day07.hotel;

import java.util.Scanner;

public class HotelSystem {

	Hotel hotel;

	public HotelSystem() {
		this.hotel = new Hotel();
	}

	public void menu() {
		while (true) {
			System.out.println("1. 查看客房状态");
			System.out.println("2. 办理入住");
			System.out.println("3. 办理退房");
			System.out.println("0. 退出程序");
			System.out.println("请选择");
			switch (new Scanner(System.in).nextLine()) {
				case "1":
					show();
					break;
				case "2":
					checkin();
					break;
				case "3":
					checkout();
					break;
				case "0":
					System.out.println("再见!");
					return;
				default:
					System.out.println("选择错误");
			}
		}
	}

	public void show() {
		// Room[][] rooms = this.hotel.rooms;
		for (int i = 0; i < this.hotel.rooms.length; i++) {
			for (int j = 0; j < this.hotel.rooms[i].length; j++) {
				Room r = this.hotel.rooms[i][j];
				System.out.print(r.num + "\t");
			}
			System.out.println();
			for (int j = 0; j < this.hotel.rooms[i].length; j++) {
				Room r = this.hotel.rooms[i][j];
				System.out.print((r.empty ? "空闲" : r.customer.name) + "\t");
			}
			System.out.println("\r\n");
		}
	}

	public void checkin() {
		// 提示并接收选择房间
		System.out.println("请选择房间");
		int x = new Scanner(System.in).nextInt();

		// 判断所选房间是否有效
		int r = x / 100 - 1;
		int c = x % 100 - 1;
		if (r < 0 || r > this.hotel.rooms.length - 1 || c < 0 || c > this.hotel.rooms[r].length - 1) {
			System.out.println("所选房间无效");
			return;
		}

		// 判断所选房间是否为空
		if (!this.hotel.rooms[r][c].empty) {
			System.out.println("所选房间已有人入住");
			return;
		}

		// 录入客人姓名存入到房间中
		System.out.println("请输入客人姓名");
		String name = new Scanner(System.in).nextLine();
		Customer cus = new Customer(name);
		this.hotel.rooms[r][c].customer = cus;
		this.hotel.rooms[r][c].empty = false;
		System.out.println("入住成功");
	}

	public void checkout() {
		// 提示并接收选择房间
		System.out.println("请选择房间");
		int x = new Scanner(System.in).nextInt();

		// 判断所选房间是否有效
		int r = x / 100 - 1;
		int c = x % 100 - 1;
		if (r < 0 || r > this.hotel.rooms.length - 1 || c < 0 || c > this.hotel.rooms[r].length - 1) {
			System.out.println("所选房间无效");
			return;
		}

		// 判断所选房间是否为空
		if (this.hotel.rooms[r][c].empty) {
			System.out.println("所选房间无人入住");
			return;
		}
		// this.hotel.rooms[r][c] = null;
		this.hotel.rooms[r][c].customer = null;
		this.hotel.rooms[r][c].empty = true;
		System.out.println("退房成功");
	}

}
